package jch.learn.java.advanced.chapter2_InputAndOutput;

import java.io.*;

public class Test2 {

    public static void main(String[] args) {
        String path = Test2.class.getResource("/data.txt").getPath();
        try {
            PrintWriter out = new PrintWriter(path, "UTF-8");
            // 等价于
            PrintWriter out1 = new PrintWriter(new FileOutputStream(path));

            String name = "test";
            double d = 12;
            out.print(name);
            out.print(' ');
            out.print(d);
            out.flush(); // 将内容从刷写入到 data.txt 文件中

            out1.print("zhangshan");
            out1.print(1);
            out1.flush();

            autoFlush();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }


    private static void autoFlush(){
        try {
            // 自动冲刷 写入文件
            PrintWriter out = new PrintWriter(
                    new OutputStreamWriter(new FileOutputStream(Test2.class.getResource("/data.txt").getPath()),"UTF-8"),true
            );
            out.println("写入文字");
            out.println(1234);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
